package simonxianyu.boot.mybatis.interceptor;

import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;
import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory;
import org.springframework.data.domain.Pageable;

abstract public class AbstractPaginationInterceptor {
  DefaultObjectFactory defaultObjectFactory = new DefaultObjectFactory();
  DefaultObjectWrapperFactory defaultObjectWrapperFactory = new DefaultObjectWrapperFactory();
  DefaultReflectorFactory defaultReflectorFactory= new DefaultReflectorFactory();

  Pageable makePageable(Object parameterObject) {
      Pageable pagination = null;

      if(parameterObject instanceof MapperMethod.ParamMap){
          MapperMethod.ParamMap paramMapObject = (MapperMethod.ParamMap)parameterObject ;
          for(Object key : paramMapObject.keySet()){
              if(paramMapObject.get(key) instanceof  Pageable){
                  pagination = (Pageable) paramMapObject.get(key);
                  break;
              }
          }
      }
      return pagination;
  }
}
